using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.IO;

namespace CEQuery
{
    public partial class CreateDB : Form
    {
        public CreateDB()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; private set; }

        private bool ValidateInput()
        {
            bool isValid = true;
            if (this.txtFile.Text.Trim() == string.Empty)
            {
                isValid = false;
                errValidation.SetError(btnSelect, "Select a file.");
            }
            else
            {
                errValidation.SetError(btnSelect, string.Empty);
            }
            return isValid;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            saveFileDialog.Title = "Create the SQL CE File";
            saveFileDialog.Filter = "SQL CE Files (*.sdf)|*.sdf";
            saveFileDialog.RestoreDirectory = true;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtFile.Text = saveFileDialog.FileName;
                txtPassword.Focus();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!ValidateInput())
            {
                return;
            }
            try
            {
                //string conn = string.Format("Data Source={0};Password={1};Max Database Size=4091;Max Buffer Size = 1024;Default Lock Escalation =100;Encrypt Database=TRUE", txtFile.Text, txtPassword.Text);
                string conn = string.Format("Data Source={0};Password={1};Max Database Size=4091;Max Buffer Size = 1024;Default Lock Escalation =100;Encrypt Database={2}", txtFile.Text, txtPassword.Text, txtPassword.Text.Length == 0 ? "FALSE" : "TRUE");
                using (SqlCeConnection localConnection = new SqlCeConnection(conn))
                {
                    if (!File.Exists(localConnection.Database))
                    {
                        using (SqlCeEngine engine = new SqlCeEngine(conn))
                        {
                            engine.CreateDatabase();
                            if (txtSchema.Text != string.Empty)
                            {
                                using (SqlCeConnection connection = new SqlCeConnection(conn))
                                {
                                    string[] commands = File.ReadAllText(txtSchema.Text).Split(';');
                                    SqlCeCommand cmd = new SqlCeCommand();
                                    cmd.Connection = connection;
                                    connection.Open();
                                    foreach (string command in commands)
                                    {
                                        if (command.Replace("\r\n", string.Empty).Trim() != string.Empty)
                                        {
                                            cmd.CommandText = command;
                                            cmd.ExecuteNonQuery();
                                        }
                                    }
                                }
                            }
                        }
                        MessageBox.Show("DB Created successfully.");                        
                    }
                    else
                    {
                        MessageBox.Show("DB already exists.");
                    }
                }
                btnHiddenOK.PerformClick();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSchema_Click(object sender, EventArgs e)
        {
            openFileDialog.Title = "Open the SQL CE Schema files";
            openFileDialog.Filter = "SQL CE Schema Files (*.sqlce)|*.sqlce";
            openFileDialog.RestoreDirectory = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtSchema.Text = openFileDialog.FileName;
            }
        }

    }
}